package com.gcxy.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gcxy.conf.result.R;
import com.gcxy.entity.City;
import com.gcxy.mapper.CityMapper;
import com.gcxy.service.ICityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础表-省市区 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-25
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {

    @Override
    public R getAllList() {
        return R.Success(this.list());
    }

    @Override
    public R getProvinceList() {
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(City::getLevel,1);
        return R.Success(this.list(queryWrapper));
    }



    @Override
    public R getCityList(String id) {
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(City::getLevel,2)
                .eq(City::getParentId,id);
        return R.Success(this.list(queryWrapper));
    }

    @Override
    public R getDistrictList(String id) {
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(City::getLevel,3)
                .eq(City::getParentId,id);
        return R.Success(this.list(queryWrapper));
    }

    @Override
    public R getCascadeList() {
        // 获取省级数据
        LambdaQueryWrapper<City> provinceQueryWrapper = new LambdaQueryWrapper<>();
        provinceQueryWrapper.eq(City::getLevel, 1);
        List<City> provinces = this.list(provinceQueryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (City province : provinces) {
            Map<String, Object> provinceMap = new HashMap<>();
            provinceMap.put("children", new ArrayList<Map<String, Object>>());
            provinceMap.put("label", province.getName());
            provinceMap.put("value", province.getId());

            // 获取市级数据
            LambdaQueryWrapper<City> cityQueryWrapper = new LambdaQueryWrapper<>();
            cityQueryWrapper.eq(City::getLevel, 2)
                    .eq(City::getParentId, province.getId());
            List<City> cities = this.list(cityQueryWrapper);
            List<Map<String, Object>> cityList = new ArrayList<>();
            for (City city : cities) {
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("children", new ArrayList<Map<String, Object>>());
                cityMap.put("label", city.getName());
                cityMap.put("value", city.getId());

                // 获取区级数据
                LambdaQueryWrapper<City> districtQueryWrapper = new LambdaQueryWrapper<>();
                districtQueryWrapper.eq(City::getLevel, 3)
                        .eq(City::getParentId, city.getId());
                List<City> districts = this.list(districtQueryWrapper);
                List<Map<String, Object>> districtList = districts.stream()
                        .map(district -> {
                            Map<String, Object> districtMap = new HashMap<>();
                            districtMap.put("label", district.getName());
                            districtMap.put("value", district.getId());
                            return districtMap;
                        })
                        .collect(Collectors.toList());
                cityMap.put("children", districtList);
                cityList.add(cityMap);
            }
            provinceMap.put("children", cityList);
            result.add(provinceMap);
        }
        return R.Success(result);
    }

    @Override
    public R getIdByName(String name) {
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(name!=null,City::getName, name);
        City city = this.getOne(queryWrapper);
        return R.Success(city);
    }
}
